extends Node2D


signal hp_change(h,h_max)
signal forward_complete
signal hit_complete
signal die_complete

var face=1 #1向右 -1向左
var chess=null
var align=0 #阵营
var Name=""
enum {S_idle,S_forward,S_hit}
var state=S_idle setget state_set
var hp=10 setget hp_set
var maxHp=10 setget maxHp_set
var timer=0
var die=false

var armor=0 setget armor_set #护甲值

onready var imageSprite=$Sprite

func state_set(value):
	match state:
		S_forward:
			emit_signal("forward_complete")
		S_hit:
			emit_signal("hit_complete")
	
	state=value
	
	pass

func hp_set(value):
	hp=value
	emit_signal("hp_change",hp,maxHp)
func maxHp_set(value):
	maxHp=value
	emit_signal("hp_change",hp,maxHp)
func armor_set(value):
	armor=value
	#更新护甲图标
	if armor>0:
		$"护盾".visible=true
	else:
		$"护盾".visible=false
	$"护盾/护盾text".text=String(armor)
	
func _ready():
	#connect("hp_change",$"../HpBar","onHpChange")
	pass

func _process(delta):
	match state:
		S_idle:
			comeBack(delta)
			pass
		S_forward:
			position+=Vector2(face*100*delta,0)
			timer-=delta
			if(timer<=0):
				state_set(S_idle)
			pass
		S_hit:
			position-=Vector2(face*100*delta,0)
			timer-=delta
			if(timer<=0):
				state_set(S_idle)
			
	if die:
		modulate.a-=delta
		if modulate.a<=0:
			emit_signal("die_complete")
			queue_free()
	pass
func forward():

	timer=0.3
	state_set(S_forward)
	
	pass
func hit(dmg):
	#跳数字
	var dmgLabel=load("res://Objects/dmgLabel.tscn").instance()
	Global.Battle.add_child(dmgLabel)
	dmgLabel.start(dmg)
	dmgLabel.global_position=global_position
	#改变hp
	#self.hp=hp_
	
	timer=0.15
	state_set(S_hit)
func die():
	die=true

func comeBack(delta):
	if position.length()>100*delta:
		position-=position.normalized()*100*delta
	else:
		position=Vector2()
	pass
func loadChess(chess_):
	self.chess=chess_
	self.Name=chess_.Name
	self.maxHp=chess_.maxHp
	self.hp=chess_.hp
	self.align=chess_.align
	self.armor=chess_.armor
	imageSprite.texture=load("res://Sprites/ChessImage/"+chess_.image+".png")
	if align==0:
		face=1
	elif align==1:
		face=-1
	activeLayer()
	chess_.connect("die",self,"onChessDie")
	pass
func activeLayer():
	match align:
		0:
			$Area2D.collision_layer=4
		1:
			$Area2D.collision_layer=8
	pass

#卡牌指向这个view
func select():
	$"指示箭头".visible=true
func unSelect():
	$"指示箭头".visible=false

#鼠标指向这个view
func point():
	$"选中框".visible=true
	pass
func dePoint():
	$"选中框".visible=false


#----回调----
func onChessDie():
	$Area2D.collision_layer=0